package com.kx.center.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.kx.center.common.CustomHashMap;
import com.kx.center.entity.Goods;
import com.kx.center.entity.Stockin;
import com.kx.center.entity.StockinDetail;
import com.kx.center.entity.query.QueryResult;
import com.kx.center.entity.query.QueryStockin;
import com.kx.center.mappers.GoodsMapper;
import com.kx.center.mappers.StockinMapper;
import com.kx.center.service.StockinService;

@Service
public class StockinServiceImpl implements StockinService {

	@Autowired
	StockinMapper mapper;

	@Autowired
	GoodsMapper goodsMapper;

	@Override
	public void insertStockin(Stockin entity) {
		mapper.insertStockin(entity);
	}

	@Override
	public void insertStockinDetail(StockinDetail entity) {
		mapper.insertStockinDetail(entity);
	}

	@Override
	public void updateStockin(Stockin entity) {
		mapper.updateStockin(entity);
	}

	@Override
	public void updateStockinDetail(StockinDetail entity) {
		mapper.updateStockinDetail(entity);
	}

	@Override
	public List<Stockin> findStockinByStoreId(Integer storeId) {
		return mapper.findStockinByStoreId(storeId);
	}

	@Override
	public Stockin findStockinById(Integer id) {
		return mapper.findStockinById(id);
	}

	@Override
	public List<StockinDetail> findStockinDetailBySId(Integer sid) {
		List<StockinDetail> result = new ArrayList<StockinDetail>();
		List<StockinDetail> list = mapper.findStockinDetailBySId(sid);
		
		for(StockinDetail item : list) {
			if(item.getOrderQuantity() > 0 || item.getQuantity() > 0 || item.getLoss() > 0)
				result.add(item);
		}
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void saveStockin(Stockin stockin, List<HashMap<String, Object>> json) {
		List<Goods> goods = goodsMapper.findGoodsByStoreId(stockin.getStoreId());
		HashMap<Integer, String> skuMapping = new HashMap<Integer, String>();

		for(Goods item : goods) {
			skuMapping.put(item.getId(), item.getSku());
		}
		
		// 修改
		for (HashMap<String, Object> map : json) {
			CustomHashMap<String, Object> item = CustomHashMap.convert(map);

			Integer goodsId = item.getInteger("goods_id");
			Integer quantity = item.getInteger("quantity");
			Integer stockinId = stockin.getId();

			StockinDetail entity = findStockinDetail(stockinId, goodsId);
			
			if(entity == null) {
				entity = new StockinDetail();
				entity.setSid(stockinId);
				entity.setSn(stockin.getSn());
				entity.setGoodsId(goodsId);
				entity.setMemo(item.getString("memo"));
				entity.setQuantity(quantity);
				entity.setLoss(item.getInteger("loss"));
				entity.setOrderQuantity(0);
				entity.setGoodsSku(skuMapping.get(goodsId));
				insertStockinDetail(entity);
			} else {
				entity.setMemo(item.getString("memo"));
				entity.setQuantity(quantity);
				entity.setLoss(item.getInteger("loss"));
				updateStockinDetail(entity);
			}
		}

		updateStockin(stockin);
	}

	@Override
	public void confirm(Stockin stockin) {

		// 设置库存
		Integer storeId = stockin.getStoreId();

		List<Goods> goods = goodsMapper.findGoodsByStoreId(stockin.getStoreId());

		HashMap<Integer, Integer> storageMapping = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> refStorageMapping = new HashMap<Integer, Integer>();

		for (Goods item : goods) {
			storageMapping.put(item.getId(), item.getStorage());
			refStorageMapping.put(item.getId(), item.getRefStorage());
		}
		
		List<StockinDetail> list = mapper.findStockinDetailBySId(stockin.getId());

		for (StockinDetail detail : list) {
			Integer goodsId = detail.getGoodsId();
			Integer quantity = detail.getQuantity();

			Integer storage = storageMapping.get(goodsId) + quantity;
			Integer refStorage = refStorageMapping.get(goodsId) + quantity;
			goodsMapper.updateGoodsStorage(storeId, goodsId, storage);
			goodsMapper.updateGoodsRefStorage(storeId, goodsId, refStorage);
		}
		
		updateStockin(stockin);
	}

	@Override
	public QueryResult queryStockin(QueryStockin query) {
		QueryResult result = new QueryResult();
		result.setPage(query.getPage());
		result.setPageSize(query.getPageSize());
		result.setTotalRecord(mapper.queryStockinCount(query));
		result.setData(mapper.queryStockin(query));
		return result;
	}

	@Override
	public Stockin findStockinBySN(String sn) {
		return mapper.findStockinBySN(sn);
	}

	@Override
	public StockinDetail findStockinDetail(Integer stockinId, Integer goodsId) {
		return mapper.findStockinDetail(stockinId, goodsId);
	}
}
